package com.junjie.checkin.handller;

import com.junjie.checkin.exception.*;
import com.junjie.checkin.exception.checkIn.ErrorCheckInTimeException;
import com.junjie.checkin.exception.checkIn.UserAlreadyCheckInException;
import com.junjie.checkin.exception.checkIn.UserNotSignUpException;
import com.junjie.checkin.exception.exchange.GiftUnderStockException;
import com.junjie.checkin.exception.exchange.InsufficientPointsException;
import com.junjie.checkin.exception.signUp.HistoryCancelledException;
import com.junjie.checkin.exception.signUp.SignUpCountMaxException;
import com.junjie.checkin.exception.signUp.SignUpErrorException;
import com.junjie.checkin.exception.signUp.UserAlreadySignUpException;
import com.junjie.checkin.response.Result;
import com.junjie.checkin.response.ResultStatus;
import com.junjie.checkin.security.exception.TokenInvalidException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
* @description 
* @author 洪浚杰
* @date 2025/6/9 19:59
*/
@RestControllerAdvice
public class AuthExceptionHandler {
    // 角色错误异常处理
    @ExceptionHandler(RoleErrorException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result roleErrorExceptionHandle(){
        return Result.getResult(ResultStatus.ROLE_ERROR);
    }

    // 请求参数异常处理
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result MethodArgumentNotValidExceptionHandel(MethodArgumentNotValidException e){
        String errorMessage = e.getBindingResult()
                .getAllErrors()
                .get(0)
                .getDefaultMessage();
        return  Result.getResult(ResultStatus.BAD_REQUEST,errorMessage);
    }

    @ExceptionHandler(UserAlreadyExistsException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result UserAlreadyExistsExceptionHandle(){
        return  Result.getResult(ResultStatus.USER_EXISTED);
    }

    @ExceptionHandler(TokenInvalidException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result TokenInvalidExceptionHandler(){
        return  Result.getResult(ResultStatus.TOKEN_INVALID);
    }

    @ExceptionHandler(ResourceNotFountException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result ResourceNotFountExceptionHandler(){
        return  Result.getResult(ResultStatus.RESOURCE_NOT_FOUND);
    }

    @ExceptionHandler(InvalidTimeRangeException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result InvalidTimeRangeExceptionHandler(){
        return  Result.getResult(ResultStatus.BAD_REQUEST,"开始时间必须早于结束时间");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result HttpMessageNotReadableExceptionHandler(){
        return  Result.getResult(ResultStatus.BAD_REQUEST,"枚举类型错误");
    }

    @ExceptionHandler(SignUpErrorException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result CheckInErrorExceptionHandler(){
        return Result.getResult(ResultStatus.SIGN_UP_ERROR);
    }

    @ExceptionHandler(UserAlreadySignUpException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result UserAlreadySignUpExceptionHandler(){
        return Result.getResult(ResultStatus.USER_ALREADY_SIGN_UP);
    }

    @ExceptionHandler(DeleteOwnException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result DeleteOwnExceptionHandler(){
        return Result.getResult(ResultStatus.DELETE_OWN_ERROR);
    }

    @ExceptionHandler(SignUpCountMaxException.class)
    @ResponseStatus(HttpStatus.LOCKED)
    public Result SignUpCountMaxExceptionHandler(){
        return Result.getResult(ResultStatus.SIGN_UP_COUNT_MAX);
    }

    @ExceptionHandler(UserNotSignUpException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Result UserNotSignUpExceptionHandler() {
        return Result.getResult(ResultStatus.USER_NOT_SIGN_UP);
    }


    @ExceptionHandler(ErrorCheckInTimeException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Result ErrorCheckInTimeExceptionHandler() {
        return Result.getResult(ResultStatus.CHECK_IN_ERROR);
    }

    @ExceptionHandler(UserAlreadyCheckInException.class)
    @ResponseStatus(HttpStatus.OK)
    public Result UserAlreadyCheckInExceptionHandler() {
        return Result.getResult(ResultStatus.USER_ALREADY_CHECK_IN);
    }

    @ExceptionHandler(InsufficientPointsException.class)
    @ResponseStatus(HttpStatus.LOCKED)
    public Result InsufficientPointsExceptionHandler() {
        return Result.getResult(ResultStatus.INSUFFICIENT_POINTS);
    }

    @ExceptionHandler(GiftUnderStockException.class)
    @ResponseStatus(HttpStatus.LOCKED)
    public Result GiftUnderStockExceptionHandler() {
        return Result.getResult(ResultStatus.GIFT_UNDER_STOCK);
    }

    @ExceptionHandler(OperationsBeyondPermissionException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Result OperationsBeyondPermissionExceptionHandler() {
        return Result.getResult(ResultStatus.OPERATIONS_BEYOND_PERMISSION);
    }

    @ExceptionHandler(PointsDeductionException.class)
    @ResponseStatus(HttpStatus.LOCKED)
    public Result PointsDeductionExceptionHandler() {
        return Result.getResult(ResultStatus.POINT_DEDUCTION_ERROR);
    }

    @ExceptionHandler(HistoryCancelledException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result HistoryCancelledExceptionHandler() {
        return Result.getResult(ResultStatus.HISTORY_CANCELLED);
    }

    @ExceptionHandler(GiftAlreadyRemoveException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result GiftAlreadyRemoveExceptionHandler() {
        return Result.getResult(ResultStatus.GIFT_ALREADY_REMOVE);
    }
}
